package com.siblings.loan.ui.steps.act

import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.lib.base.base.BaseActivity
import com.lib.base.utils.thread.TimingThread
import com.siblings.loan.R
import com.siblings.loan.databinding.ActStepCheckWaitBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventData
import com.siblings.loan.utils.event.SibEventType
import org.greenrobot.eventbus.EventBus
import kotlin.random.Random

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/12 18:45
 * @Annotation：流程审查中
 */
class SibStepCheckWaitAct: BaseActivity(),TimingThread.ITimingThreadListener {
    lateinit var binding: ActStepCheckWaitBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 9
    override fun getSibRootView(): View {
        binding = ActStepCheckWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setSibTextTitle("")
        setSibBackBtnVisibility(View.INVISIBLE)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)

    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Information Step Wait Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER, "Information Step Wait Page")
        }
    }

    override fun initSibData() {
        startTimeThread()

//        val string = "Your application is being reviewed in its entirety. It takes as little as 10 seconds, so don't leave this page!"
//        val strKey = "10 seconds"
//        val strKey2 = "don't leave this page!"
//
//        CoroutineScope(SupervisorJob() + Dispatchers.Main).launch {
//            TxtTAType.TxtTransformBType(this@SibStepCheckWaitAct,string)
//                .setTargetBType(strKey)
//                .setColorBType(R.color.purple_200)
//                .matchBType()
//                .setTargetBType(strKey2)
//                .setColorBType(R.color.purple_700)
//                .matchBType()
//                .updateBType(binding.textWaitTips)
//        }
    }

    /**
     * 等待2秒 关闭页面
     */
    private fun onSibWaitingFinish(){
        Handler(Looper.getMainLooper()).postDelayed({
            //发送通知显示引导页
            EventBus.getDefault().post(SibEventData(SibEventData.TYPE.SIB_SHOW_GUIDE_DIALOG))
            hideSibLoading()
            finish()
        },500)
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        countdown = 9
        showSibTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 0){
            countdown = 9
            timeThread!!.stopThread()
            showSibLoading(isTOuchOut = false, isCancel = true)
            onSibWaitingFinish()
            return
        }
        countdown--
        showSibTipsText()
    }

    /**
     * 设置文本
     */
    private fun showSibTipsText(){
        val textSeconds = String.format(getString(R.string.step_check_wait_tips_seconds),"$countdown")
        val textWarning = getString(R.string.step_check_wait_tips_warning)
        val showText = String.format(getString(R.string.step_check_wait_tips),textSeconds,textWarning)

        val secondsIndex = showText.indexOf(textSeconds)
        val warningIndex = showText.indexOf(textWarning)

        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF9B25")), secondsIndex, secondsIndex + textSeconds.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF3509")), warningIndex, warningIndex + textWarning.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textWaitTips.text = spString

        showSibStepStatusView()
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    /**
     * 显示状态
     */
    private fun showSibStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step2)
            binding.vStep1Right.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep2Left.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step3)
            binding.vStep2Right.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep3Left.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step3)
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step1)
            binding.vStep1Right.setBackgroundColor(Color.parseColor("#FF4DB673"))

            binding.vStep2Left.setBackgroundColor(Color.parseColor("#FF4DB673"))
            binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step2)
            binding.vStep2Right.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep3Left.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step3)
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step1)
        binding.vStep1Right.setBackgroundColor(Color.parseColor("#FF4DB673"))

        binding.vStep2Left.setBackgroundColor(Color.parseColor("#FF4DB673"))
        binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step1)
        binding.vStep2Right.setBackgroundColor(Color.parseColor("#FF4DB673"))

        binding.vStep3Left.setBackgroundColor(Color.parseColor("#FF4DB673"))
        binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step2)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_STEP_CHECK_WAIT_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_STEP_CHECK_WAIT_END, "")
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}